import java.util.Stack;

public class decodeString {
    public String decodeString(String s) {
        StringBuilder currentString = new StringBuilder();
        Stack<StringBuilder> stringStack = new Stack<>();
        Stack<Integer> numberStack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                int num = 0;
                while (i < s.length() && Character.isDigit(s.charAt(i))) {
                    num = num * 10 + (s.charAt(i) - '0');
                    i++;
                }
                numberStack.push(num);
                i--; // 回退一位，因为后面的循环会i++
            } else if (c == '[') {
                stringStack.push(currentString);
                currentString = new StringBuilder();
            } else if (c == ']') {
                StringBuilder temp = currentString;
                currentString = stringStack.pop();
                currentString.append(temp.toString().repeat(numberStack.pop()));
            } else {
                currentString.append(c);
            }
        }
        return currentString.toString();
    }
}
